﻿using HZYProject.Core.Repositories;
using HZYProject.Domain;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Linq;
using Microsoft.EntityFrameworkCore;

namespace HZYProject.Service.CollegeServices
{
    public class CollegeService : ICollegeService
    {
        private readonly IRepository<Colleges, Guid> _repository;

        public CollegeService(IRepository<Colleges, Guid> repository)
        {
            _repository = repository;
        }

        public async Task<PagedResultDto<Colleges>> GetPaginatedResult(GetCollegeInput input)
        {
            var query = _repository.GetAll();
            //搜索
            if (!string.IsNullOrEmpty(input.Name))
            {
                query = query.Where(x => x.Name.Contains(input.Name));
            }
            //统计查询数据的总条数，用于分页计算总页数
            var count = query.Count();
            //根据需求进行排序，然后进行分页逻辑的计算
            query = query.OrderByDescending(x => x.Name).Skip((input.CurrentPage - 1) * input.MaxResultCount).Take(input.MaxResultCount);
            //将查询结果转换为List集合，加载到内存中
            var models = await query.AsNoTracking().ToListAsync();
            var dtos = new PagedResultDto<Colleges>
            {
                TotalCount = count,
                CurrentPage = input.CurrentPage,
                MaxResultCount = input.MaxResultCount,
                Data = models,
                FilterText = input.FilterText,
            };
            return dtos;
        }

        /// <summary>
        /// 获取所有院校信息
        /// </summary>
        /// <returns></returns>
        //public List<SelectListItem> GetCollegesName()
        //{
        //    var list = _repository.GetAll().ToList();
        //    List<SelectListItem> arr = new List<SelectListItem>();
        //    foreach (var item in list)
        //    {
        //        arr.Add(new SelectListItem { Text = item.Name, Value = item.CollegesID.ToString() });
        //    }
        //    return arr;
        //}
    }
}
